// 引入配置
import {tip,goto, goLogin} from "@/utils"
import {Method,ErrorCode} from "@/api/config/enum"
var env = import.meta.env
const baseUrl = env.VITE_API_URL
const appId = env.VITE_APP_ID
// 请求拦截器 用于请求前和响应后做一些处理
// 拦截器
const skipInterceptor = [
  'https://restapi.amap.com/v3/geocode/regeo' // 添加高德地图的完整 URL
];
uni.addInterceptor('request', {
	// 高德地图跳过拦截器
	invoke: args => {
		const shouldSkip = skipInterceptor.some(path => args.url.includes(path));
		    if (shouldSkip) {
		      return args; 
		}
		args.url = baseUrl + args.url
		args.header = {
			'Content-Type': 'application/json', 
			'appid':appId,
			...args.header,
		}
		const token = uni.getStorageSync('token')
		if (token) {
			args.header = {
				...args.header,
				"token": token,
			}
		}
		if (args.data) {
			for (let key in args.data) {
				if (Array.isArray(args.data[key])) {
					args.data[key] = args.data[key].join(',');
				}
			}
		}
		return args
	},
	success: res => {
		console.log("拦截响应",res)
		return getStatusType(res)
	},
})
const getStatusType = (res:any) => {
	const status = res.statusCode
	let message = ''
	switch (status) {
		case 200:
			message = '请求成功'
			break
		case 400:
			message = '请求错误'
			break
		case 401:
			message = '请登录'
			tip(message,()=>{
				goto('user/login?logout=1')
			})
			break
		case 403:
			message = '拒绝访问'
			break
		case 404:
			message = `请求地址出错: ${res.url}`
			break
		case 408:
			message = '请求超时'
			break
		case 500:
			message = '服务器内部错误'
			break
		case 501:
			message = '服务未实现'
			break
		case 502:
			message = '网关错误'
			break
		case 503:
			message = '服务不可用'
			break
		case 504:
			message = '网关超时'
			break
		case 505:
			message = 'HTTP版本不受支持'
			break
		default:
			message = '请求失败'
	}
	if (status !== 200) {
		tip(message)
	}
	return res.data
}
export const request = (url:string,method:Method,params={},header={})=>{
	return new Promise((resolve?:any, reject?:any) => {
		uni.request({
			url: url,
			method: method,
			data: params,
			header: {
				...header,
			},
			success: (res:any) => {
				let data = res.data		//这里的code不是response里的code，是请求成功后返回数据里的自定义code
				if(data.code==ErrorCode.noLogin){
					tip("请登录",()=>{
						goLogin()
					})
				}
				else if(data.code!=ErrorCode.success){
					//tip(data.msg)
					reject(data.msg)
				}else{
					resolve(data.data)
				}
			},
			fail: (err:any) => {
				reject(err.errMsg)
			},
		})
	})
}

export const upload = (file:string,formData?:any)=>{
	return new Promise((resolve?:Function, reject?:Function) => {
		uni.uploadFile({
			url:baseUrl + 'file.Upload/image',
			filePath: file,
			name: 'iFile',
			header:{
				'appId':appId,
				'token': uni.getStorageSync('token'),
			},
			formData: {
				app_id:appId,
				...formData
			},
			success: (res:any) => {
				let data = getStatusType(res)
				data = JSON.parse(data)
				if(data.code!=ErrorCode.success){
					tip(data.msg)
					if(typeof reject=='function'){
						console.log(111111,data)
						reject(data.msg)
					}
				}else{
					if(typeof resolve=='function'){
						console.log(222222,data)
						resolve(data.data)
					}
				}
			},
			fail: (err:any) => {
				tip(err.errMsg)
				if(typeof reject=='function'){
					reject()
				}
			},
		})
	})
}